package txt.livewallpaper;

import android.app.Service;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Handler;
import android.os.IBinder;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import java.util.Random;

import butterknife.BindColor;

public class LiveWallpaperService extends WallpaperService {
    private final String TAG = LiveWallpaperService.class.getSimpleName();

    private final int[] mColorsResId = new int[]{
            R.color.blue,
            R.color.colorAccent,
            R.color.colorPrimaryDark,
            R.color.green,
            R.color.purple,
            R.color.yellow,
            R.color.grey,
            R.color.lite_blue,
            R.color.orange,
            R.color.red
    };

    public LiveWallpaperService() {
    }

    @Override
    public Engine onCreateEngine() {
        return new MyEngine();
    }


    private class MyEngine extends Engine {
        private Paint mPaint;

        private boolean FLAG_VISIBLE = false;

        private Handler mHandler = new Handler();
        private Runnable mDrawFrameRunnable = new Runnable() {
            @Override
            public void run() {
                drawFrame();
            }
        };

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setAntiAlias(true);

            setTouchEventsEnabled(true);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mHandler.removeCallbacksAndMessages(null);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            FLAG_VISIBLE = visible;
            if (FLAG_VISIBLE) {
                drawFrame();
            } else {
                mHandler.removeCallbacksAndMessages(null);
            }
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
            super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset);
            Log.i(TAG, "onOffsetsChanged: xOffset=" + xOffset + ", yOffset=" + yOffset +
                    ", xOffsetStep=" + xOffsetStep + ", yOffsetStep=" + yOffsetStep +
                    ", xPixelOffset=" + xPixelOffset + ", yPixelOffset=" + yPixelOffset);
        }

        @Override
        public void onTouchEvent(MotionEvent event) {
            Log.i(TAG, "onTouchEvent: Receie touch event: " + event);

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mCircleCenterX = event.getX();
                mCircleCenterY = event.getY();

                mPaint.setColor(getResources().getColor(mColorsResId[(int) (Math.random()*10)]));

//                if (event.getX() < 200 && event.getY() < 200) {
//                    //clear all line
//                    clearFrame();
//                }
            }
            super.onTouchEvent(event);
        }

        private float mCircleCenterX = 100 , mCircleCenterY = 400;

        private void drawFrame() {
            Canvas canvas = null;
            try {
                canvas = getSurfaceHolder().lockCanvas();
                canvas.drawCircle(mCircleCenterX, mCircleCenterY, 50, mPaint);

            } finally {
                getSurfaceHolder().unlockCanvasAndPost(canvas);
            }

            if (FLAG_VISIBLE) {
                mCircleCenterY += 1;
                if (mCircleCenterY >= getResources().getDisplayMetrics().heightPixels) {
                    mPaint.setColor(getResources().getColor(mColorsResId[(int) (Math.random() * 10)]));
                    mCircleCenterX = (float) (Math.random() * getResources().getDisplayMetrics().widthPixels - 100);
                    mCircleCenterY = (float) (Math.random() * getResources().getDisplayMetrics().heightPixels);
                }
                mHandler.postDelayed(mDrawFrameRunnable, 10);
            }
        }

        private void clearFrame() {
            Canvas canvas = null;
            try {
                canvas = getSurfaceHolder().lockCanvas();
                canvas.drawColor(0xff000000);
            } finally {
                getSurfaceHolder().unlockCanvasAndPost(canvas);
            }

            mPaint.setXfermode(null);
        }
    }
}

